package com.dreamlike.nlp.Configuartion;

import io.vertx.mysqlclient.MySQLConnectOptions;
import io.vertx.mysqlclient.SslMode;
import io.vertx.sqlclient.PoolOptions;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.Map;

@Configuration(proxyBeanMethods = false)
@ConfigurationProperties(prefix = "mysql")
@Data
public class MySQLDataSourceConfiguration {
    private String host = "localhost";
    private int port = 3306;
    private String username = "root";
    private String password = "";
    private String database = "";
    private String charset = "utf8mb4";
    private boolean useAffectedRows = true;
    private Map<String, String> connectionAttributes = Map.of("_client_name", "vertx-mysql-client");
    private String sslMode = "disabled";
    private String characterEncoding = "UTF-8";
    private boolean cachePreparedStatements = true;
    private int preparedStatementCacheMaxSize = 256;
    private int preparedStatementCacheSqlLimit = 2048;


    private int poolMaxSize = 4;
    private int poolMaxWaitQueueSize = -1;


    @Bean
    public MySQLConnectOptions mySQLConnectOptions(){
        MySQLConnectOptions options = new MySQLConnectOptions();
        options.setHost(host)
                .setCachePreparedStatements(cachePreparedStatements)
                .setPreparedStatementCacheMaxSize(preparedStatementCacheMaxSize)
                .setPreparedStatementCacheSqlLimit(preparedStatementCacheSqlLimit)
                .setPort(port)
                .setUser(username)
                .setPassword(password)
                .setDatabase(database)
                .setCharset(charset)
                .setUseAffectedRows(useAffectedRows)
                .setSslMode(SslMode.of(sslMode))
                .setCharacterEncoding(characterEncoding);
        return options;
    }

    @Bean
    public PoolOptions poolOptions(){
        PoolOptions poolOptions = new PoolOptions();
        poolOptions.setMaxSize(poolMaxSize)
                .setMaxWaitQueueSize(poolMaxWaitQueueSize);
        return poolOptions;
    }


}
